package models

import (
	"time"

	"github.com/google/uuid"
	"gorm.io/gorm"
)

// 聊天室模型
type Room struct {
	ID          string    `gorm:"primaryKey; type:varchar(255)" json:"id"`
	Name        string    `gorm:"type:varchar(255); not null" json:"name"`
	Description string    `gorm:"type:text" json:"description"`
	Type        string    `gorm:"type:varchar(50); default:'private'; comment:'private,group,public'" json:"type"`
	Avatar      string    `gorm:"type:varchar(255)" json:"avatar"`
	CreatorID   string    `gorm:"type:varchar(255); not null" json:"creator_id"`
	CreatedAt   time.Time `gorm:"type:timestamp" json:"created_at"`
	UpdatedAt   time.Time `gorm:"type:timestamp" json:"updated_at"`
	
	// 关联
	Creator  User         `gorm:"foreignKey:CreatorID" json:"creator"`
	Members  []RoomMember `gorm:"foreignKey:RoomID" json:"members"`
	Messages []Message    `gorm:"foreignKey:RoomID" json:"messages"`
}

func (r *Room) TableName() string {
	return "chat_room"
}

func (r *Room) BeforeCreate(tx *gorm.DB) error {
	r.ID = uuid.NewString()
	r.CreatedAt = time.Now()
	return nil
}

func (r *Room) BeforeUpdate(tx *gorm.DB) error {
	r.UpdatedAt = time.Now()
	return nil
}

// 聊天室成员模型
type RoomMember struct {
	ID        string    `gorm:"primaryKey; type:varchar(255)" json:"id"`
	RoomID    string    `gorm:"type:varchar(255); not null" json:"room_id"`
	UserID    string    `gorm:"type:varchar(255); not null" json:"user_id"`
	Role      string    `gorm:"type:varchar(50); default:'member'; comment:'member,admin,owner'" json:"role"`
	JoinedAt  time.Time `gorm:"type:timestamp" json:"joined_at"`
	IsActive  bool      `gorm:"type:boolean; default:true" json:"is_active"`
	
	// 关联
	Room Room `gorm:"foreignKey:RoomID" json:"room"`
	User User `gorm:"foreignKey:UserID" json:"user"`
}

func (rm *RoomMember) TableName() string {
	return "chat_room_member"
}

func (rm *RoomMember) BeforeCreate(tx *gorm.DB) error {
	rm.ID = uuid.NewString()
	rm.JoinedAt = time.Now()
	return nil
}

// 消息模型
type Message struct {
	ID        string    `gorm:"primaryKey; type:varchar(255)" json:"id"`
	RoomID    string    `gorm:"type:varchar(255); not null" json:"room_id"`
	UserID    string    `gorm:"type:varchar(255); not null" json:"user_id"`
	Content   string    `gorm:"type:text; not null" json:"content"`
	Type      string    `gorm:"type:varchar(50); default:'text'; comment:'text,image,file,system'" json:"type"`
	Status    string    `gorm:"type:varchar(50); default:'sent'; comment:'sent,delivered,read,failed'" json:"status"`
	ReplyTo   *string   `gorm:"type:varchar(255)" json:"reply_to"` // 回复的消息ID
	FileURL   *string   `gorm:"type:varchar(255)" json:"file_url"`  // 文件URL
	FileName  *string   `gorm:"type:varchar(255)" json:"file_name"` // 文件名
	FileSize  *int64    `gorm:"type:bigint" json:"file_size"`       // 文件大小
	CreatedAt time.Time `gorm:"type:timestamp" json:"created_at"`
	UpdatedAt time.Time `gorm:"type:timestamp" json:"updated_at"`
	
	// 关联
	Room   Room   `gorm:"foreignKey:RoomID" json:"room"`
	User   User   `gorm:"foreignKey:UserID" json:"user"`
	Reply  *Message `gorm:"foreignKey:ReplyTo" json:"reply"`
	Reads  []MessageRead `gorm:"foreignKey:MessageID" json:"reads"`
}

func (m *Message) TableName() string {
	return "chat_message"
}

func (m *Message) BeforeCreate(tx *gorm.DB) error {
	m.ID = uuid.NewString()
	m.CreatedAt = time.Now()
	return nil
}

func (m *Message) BeforeUpdate(tx *gorm.DB) error {
	m.UpdatedAt = time.Now()
	return nil
}

// 消息已读状态模型
type MessageRead struct {
	ID         string    `gorm:"primaryKey; type:varchar(255)" json:"id"`
	MessageID  string    `gorm:"type:varchar(255); not null" json:"message_id"`
	UserID     string    `gorm:"type:varchar(255); not null" json:"user_id"`
	ReadAt     time.Time `gorm:"type:timestamp" json:"read_at"`
	
	// 关联
	Message Message `gorm:"foreignKey:MessageID" json:"message"`
	User    User    `gorm:"foreignKey:UserID" json:"user"`
}

func (mr *MessageRead) TableName() string {
	return "chat_message_read"
}

func (mr *MessageRead) BeforeCreate(tx *gorm.DB) error {
	mr.ID = uuid.NewString()
	mr.ReadAt = time.Now()
	return nil
}

// 好友关系模型
type Friend struct {
	ID          string    `gorm:"primaryKey; type:varchar(255)" json:"id"`
	UserID      string    `gorm:"type:varchar(255); not null" json:"user_id"`
	FriendID    string    `gorm:"type:varchar(255); not null" json:"friend_id"`
	Status      string    `gorm:"type:varchar(50); default:'pending'; comment:'pending,accepted,blocked'" json:"status"`
	CreatedAt   time.Time `gorm:"type:timestamp" json:"created_at"`
	UpdatedAt   time.Time `gorm:"type:timestamp" json:"updated_at"`
	
	// 关联
	User   User `gorm:"foreignKey:UserID" json:"user"`
	Friend User `gorm:"foreignKey:FriendID" json:"friend"`
}

func (f *Friend) TableName() string {
	return "chat_friend"
}

func (f *Friend) BeforeCreate(tx *gorm.DB) error {
	f.ID = uuid.NewString()
	f.CreatedAt = time.Now()
	return nil
}

func (f *Friend) BeforeUpdate(tx *gorm.DB) error {
	f.UpdatedAt = time.Now()
	return nil
}

// 好友请求模型
type FriendRequest struct {
	ID          string    `gorm:"primaryKey; type:varchar(255)" json:"id"`
	SenderID    string    `gorm:"type:varchar(255); not null" json:"sender_id"`
	ReceiverID  string    `gorm:"type:varchar(255); not null" json:"receiver_id"`
	Status      string    `gorm:"type:varchar(50); default:'pending'; comment:'pending,accepted,rejected'" json:"status"`
	Message     string    `gorm:"type:text" json:"message"`
	CreatedAt   time.Time `gorm:"type:timestamp" json:"created_at"`
	UpdatedAt   time.Time `gorm:"type:timestamp" json:"updated_at"`
	
	// 关联
	Sender   User `gorm:"foreignKey:SenderID" json:"sender"`
	Receiver User `gorm:"foreignKey:ReceiverID" json:"receiver"`
}

func (fr *FriendRequest) TableName() string {
	return "chat_friend_request"
}

func (fr *FriendRequest) BeforeCreate(tx *gorm.DB) error {
	fr.ID = uuid.NewString()
	fr.CreatedAt = time.Now()
	return nil
}

func (fr *FriendRequest) BeforeUpdate(tx *gorm.DB) error {
	fr.UpdatedAt = time.Now()
	return nil
}

// 用户在线状态模型
type UserStatus struct {
	ID        string    `gorm:"primaryKey; type:varchar(255)" json:"id"`
	UserID    string    `gorm:"type:varchar(255); not null; unique" json:"user_id"`
	Status    string    `gorm:"type:varchar(50); default:'offline'; comment:'online,offline,busy'" json:"status"`
	LastSeen  time.Time `gorm:"type:timestamp" json:"last_seen"`
	CreatedAt time.Time `gorm:"type:timestamp" json:"created_at"`
	UpdatedAt time.Time `gorm:"type:timestamp" json:"updated_at"`
	
	// 关联
	User User `gorm:"foreignKey:UserID" json:"user"`
}

func (us *UserStatus) TableName() string {
	return "chat_user_status"
}

func (us *UserStatus) BeforeCreate(tx *gorm.DB) error {
	us.ID = uuid.NewString()
	us.CreatedAt = time.Now()
	return nil
}

func (us *UserStatus) BeforeUpdate(tx *gorm.DB) error {
	us.UpdatedAt = time.Now()
	return nil
}

// 聊天邀请模型
type RoomInvite struct {
	ID        string    `gorm:"primaryKey; type:varchar(255)" json:"id"`
	RoomID    string    `gorm:"type:varchar(255); not null" json:"room_id"`
	InviterID string    `gorm:"type:varchar(255); not null" json:"inviter_id"`
	InviteeID string    `gorm:"type:varchar(255); not null" json:"invitee_id"`
	Status    string    `gorm:"type:varchar(50); default:'pending'; comment:'pending,accepted,rejected'" json:"status"`
	Token     string    `gorm:"type:varchar(255); unique" json:"token"`
	ExpiresAt time.Time `gorm:"type:timestamp" json:"expires_at"`
	CreatedAt time.Time `gorm:"type:timestamp" json:"created_at"`
	
	// 关联
	Room    Room `gorm:"foreignKey:RoomID" json:"room"`
	Inviter User `gorm:"foreignKey:InviterID" json:"inviter"`
	Invitee User `gorm:"foreignKey:InviteeID" json:"invitee"`
}

func (ri *RoomInvite) TableName() string {
	return "chat_room_invite"
}

func (ri *RoomInvite) BeforeCreate(tx *gorm.DB) error {
	ri.ID = uuid.NewString()
	ri.CreatedAt = time.Now()
	return nil
}